#include "GLPipeline.h"
#include <iostream>


using namespace std;

void show_glfw_error(int error, const char* description);
void window_resized(GLFWwindow* window, int width, int height);



// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

GLPipeline::GLPipeline()
    :window(nullptr)
{
}

GLPipeline::~GLPipeline()
{
    if (window)
    {
        glfwTerminate();
        exit(-1);
    }
}

void GLPipeline::init()
{
    if (!glfwInit()) {
        std::cerr << "GLFW init failed" << '\n';
        exit(-1);
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    window = glfwCreateWindow(
        SCR_WIDTH,
        SCR_HEIGHT,
        "OpenGL Example",
        NULL,
        NULL);

    if (!window)
    {
        std::cerr << "Create window failed" << '\n';
        glfwTerminate();
        exit(-1);
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        printf("err:gladLoadGLLoader is NULL");
        return;
    }
    glfwSetWindowSizeCallback(window,window_resized);

    glfwSwapInterval(1);


    std::cout << glGetString(GL_VERSION) << endl;

}

void window_resized(GLFWwindow* window, int width, int height)
{
    if (width > 0 && height > 0)
    {
        std::cout << "Window resized, new window size: " << width << " x " << height << '\n';

        glClear(GL_COLOR_BUFFER_BIT);
        glViewport(0, 0, width, height);
        glfwSwapBuffers(window);
    }
}



void show_glfw_error(int error, const char* description)
{
    std::cerr << "Error: " << description << '\n';
}

//glm::vec3 GLPipeline::GetcameraPos() const
//{
//    return cameraPos;
//}
//
//
//glm::vec3 GLPipeline::GetcameraFront() const
//{
//    return cameraFront;
//}
//glm::vec3 GLPipeline::GetcameraUp() const
//{
//    return cameraUp;
//}